package com.party.common.utils;

/**
 * @author: zhaoyongke
 * @create: 2024-07-01 08:37
 * @description: fff
 */

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;

/**
 * &#064;Author
 * &#064;Date  Created in  2024/4/11 上午11:13
 * &#064;DESCRIPTION:   将 multipartFile --> file
 * &#064;Version  V1.0
 */
public class FileUtil {



    /**
     * 将 multipartFile -->  fileInputStream
     */
    public static FileInputStream convertMultipartFileToInputStream(MultipartFile multipartFile) throws IOException {
        // 创建临时文件
        File tempFile = File.createTempFile("temp", null);

        // 将MultipartFile的数据写入临时文件
        try (FileOutputStream fos = new FileOutputStream(tempFile)) {
            fos.write(multipartFile.getBytes());
        }

        // 将临时文件转换为FileInputStream
        FileInputStream fileInputStream = new FileInputStream(tempFile);

        // 删除临时文件
        tempFile.delete();

        return fileInputStream;
    }

    /**
     * 将上传的 logo图片保存在本地；
     */
    public static File convertInputStreamToFile(InputStream inputStream,String fileName) throws IOException {
        // 创建图片文件
        String resourcesPath = new File("E:/kk").getAbsolutePath();
        // 拼接images目录的路径
        String imagesPath =   "E:/kk/static/imagesLogo/"; // 这里的static是resources目录下的一个示例子目录，你可以根据实际情况修改
//        logger.info("我的路径是:{}"+imagesPath);
        File directory = new File(imagesPath);
        // 如果目标文件夹不存在，则创建它
        if (!directory.exists()) {
            directory.mkdirs();
        }
        File file = new File(directory, fileName);
        try (OutputStream outputStream = Files.newOutputStream(file.toPath())) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, length);
            }
        }
//        deleteImage(imagesPath,fileName);
        return file;
    }

    /**
     * 删除图片
     */
    public static void deleteImage(String directoryPath, String imageName) {
        File directory = new File(directoryPath);
        if (!directory.exists() || !directory.isDirectory()) {
            return;
        }
        File imageFile = new File(directory, imageName);
        if (imageFile.exists() && imageFile.isFile()) {
            if (imageFile.delete()) {
//                logger.info("删除图片成功！");
            } else {
//                logger.error("删除图片失败！");
            }
        } else {
            System.out.println("Image file " + imageName + " does not exist.");
        }
    }

    /**
     * 修改图片的宽高 等比例扩大 海报背景图和 logo
     */
    public static byte[] resizeImage(byte[] imageData, int width, int height) throws IOException {
        ByteArrayInputStream bis = new ByteArrayInputStream(imageData);
        BufferedImage image = ImageIO.read(bis);

        Image resizedImage = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);

        BufferedImage bufferedResizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        bufferedResizedImage.getGraphics().drawImage(resizedImage, 0, 0, null);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(bufferedResizedImage, "jpg", bos);
        byte[] resizedImageData = bos.toByteArray();

        bis.close();
        bos.close();

        return resizedImageData;
    }
//    public static void main(String[] args) throws IOException {
//        String resourcesPath = new File("src/main/resources/").getAbsolutePath();
//        // 拼接images目录的路径
//        String imagesPath = resourcesPath + "/static/images/"; // 这里的static
//
//        // 读取背景图片
//        BufferedImage backgroundImage = ImageIO.read(new File(imagesPath + "/" + "4c9a599ae920240424112637.png"));
//        ImageIO.write(backgroundImage, "PNG", new File("D:\\test1\\image.png"));
//
//    }
}
